package com.logistics.modules.vehicleManagement.service.impl;

import cn.hutool.core.convert.Convert;
import com.logistics.common.utils.R;
import com.logistics.modules.transportationManagement.chart.KeyAndValueChart;
import com.logistics.modules.transportationManagement.entity.CargoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.logistics.common.utils.PageUtils;
import com.logistics.common.utils.Query;

import com.logistics.modules.vehicleManagement.dao.VehicleDao;
import com.logistics.modules.vehicleManagement.entity.VehicleEntity;
import com.logistics.modules.vehicleManagement.service.VehicleService;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author 水穷处云起时
 */
@Service("vehicleService")
public class VehicleServiceImpl extends ServiceImpl<VehicleDao, VehicleEntity> implements VehicleService {
    @Autowired
    private VehicleDao vehicleDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //车牌号
        String key = Convert.toStr(params.get("key"));
        //车辆状态
        String vStatus = Convert.toStr(params.get("vStatus"));

        IPage<VehicleEntity> page = new Query<VehicleEntity>().getPage(params);
        vehicleDao.queryPageByStatus(page,key,vStatus);
        return new PageUtils(page);
    }

    @Override
    public List<VehicleEntity> queryList(Long[] num) {

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("id", num);
        List<VehicleEntity> list = vehicleDao.selectList(queryWrapper);
        return list;
    }

    @Override
    public PageUtils queryPageByStatus(Map<String, Object> params) {
        //车牌号
        String key = Convert.toStr(params.get("key"));
        String vStatus = Convert.toStr("1");
        IPage<VehicleEntity> page = new Query<VehicleEntity>().getPage(params);
        vehicleDao.queryPageByStatus(page,key,vStatus);
        return new PageUtils(page);
    }

    /**
     * 检查车辆是否有空闲的
     *
     * @return {@link Boolean}
     */
    @Override
    public Boolean checkIfTheVehicleIsFree() {
        int count = vehicleDao.countByVehicleStatus(1);
        return count > 0;
    }

    @Override
    public PageUtils listByStatusFalse(Map<String, Object> params) {
        //车牌号
        String key = Convert.toStr(params.get("key"));
        String vStatus = Convert.toStr("0");
        IPage<VehicleEntity> page = new Query<VehicleEntity>().getPage(params);
        vehicleDao.queryPageByStatus(page,key,vStatus);
        return new PageUtils(page);
    }

    @Override
    public R getPieData() {
        // 拿到出车中的车辆
        Integer count = this.baseMapper.countByVehicleStatus(0);
        // 拿到空闲中的车辆
        Integer count1 = this.baseMapper.countByVehicleStatus(1);
        // 封装数据
        ArrayList<KeyAndValueChart> keyAndValueCharts = new ArrayList<>();
        keyAndValueCharts.add(new KeyAndValueChart("出车中", count.toString()));
        keyAndValueCharts.add(new KeyAndValueChart("空闲中", count1.toString()));
        // 返回数据
        return R.ok().put("res", keyAndValueCharts);
    }

    /**
     * 删除检查
     *
     * @param asList 正如列表
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void deleteCheck(List<Integer> asList) {
        int size = asList.size();
        for (int i = 0; i < size; i++) {
            int count = this.baseMapper.countByVehicleTypeId(asList.get(i));
            if (count > 0) {
                throw new RuntimeException("该车辆类型已被占用! 无法删除!");
            }
        }
    }
}